# Contributor: August Klein <amatcoder@gmail.com>
# Maintainer: psykose <alice@ayaya.dev>
pkgname=sdl2
pkgver=2.0.22
pkgrel=1
pkgdesc="development library designed to provide low level access to audio, keyboard, mouse, joystick and graphics"
url="https://www.libsdl.org"
arch="all"
license="Zlib"
depends_dev="$pkgname directfb-dev mesa-dev"
makedepends="
	alsa-lib-dev
	cmake
	eudev-dev
	jack-dev
	libx11-dev
	libxcursor-dev
	libxext-dev
	libxi-dev
	libxkbcommon-dev
	libxrandr-dev
	libxrender-dev
	libxscrnsaver-dev
	libusb-dev
	pulseaudio-dev
	samurai
	sndio-dev
	wayland-dev
	wayland-protocols
	$depends_dev
	"
subpackages="$pkgname-dbg $pkgname-dev"
source="
	https://www.libsdl.org/release/SDL2-$pkgver.tar.gz
	directfb-cflags.patch
	"
options="!check" # No good way to run the available tests
builddir="$srcdir/SDL2-$pkgver"

# secfixes:
#   2.0.18-r0:
#     - CVE-2021-33657
#   2.0.10-r0:
#     - CVE-2019-7572
#     - CVE-2019-7573
#     - CVE-2019-7574
#     - CVE-2019-7575
#     - CVE-2019-7576
#     - CVE-2019-7578
#     - CVE-2019-7635
#     - CVE-2019-7636
#     - CVE-2019-7637
#     - CVE-2019-7638

case "$CARCH" in
	s390x) _pipewire=OFF ;;
	*)
		makedepends="$makedepends pipewire-dev"
		_pipewire=ON
		;;
esac

build() {
	# NOTE: Please do not remove the -DSDL_DIRECTFB=ON flag.
	# Some apps, used for booting Alpine derivatives, require this to function!
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	GIT_DIR=. \
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DBUILD_SHARED_LIBS=True \
		-DSDL_TEST=OFF \
		-DSDL_ALSA=ON \
		-DSDL_JACK=ON \
		-DSDL_CLOCK_GETTIME=ON \
		-DSDL_ARTS=OFF \
		-DSDL_ESD=OFF \
		-DSDL_NAS=OFF \
		-DSDL_RPATH=OFF \
		-DSDL_X11=ON \
		-DSDL_DIRECTFB=ON \
		-DSDL_WAYLAND=ON \
		-DSDL_STATIC=ON \
		-DSDL_SHARED=ON \
		-DSDL_PIPEWIRE_SHARED=$_pipewire \
		-DSDL_PULSEAUDIO_SHARED=ON \
		-DSDL_SNDIO_SHARED=ON \
		$CMAKE_CROSSOPTS
	ninja -C build
}

package() {
	DESTDIR="$pkgdir" ninja -C build install
}

dev() {
	default_dev

	# move back dev symlink into main package so things can dlopen it
	# libSDL2.so -> libSDL2-2.0.so -> libSDL2-2.0.so.version
	mv "$subpkgdir"/usr/lib/libSDL2.so "$subpkgdir"/usr/lib/libSDL2-2.0.so \
		"$pkgdir"/usr/lib
}

sha512sums="
ca4b690433cd4d9d73b797da98666317128e7e817ab60e874a49d94791ea41e8a6b4fc43649593120daa0702190c0f8a6ed326c908d87375c8da9f369d994f6a  SDL2-2.0.22.tar.gz
eed5537eaedec6cc2f44d6961d883a07249f2572d8d29015060ec083bc1622645db24bb8771976e86c5afca6d989a1a7b21dd44b2f519799eddd5a196ebdf018  directfb-cflags.patch
"
